//
//  TWUploadImage.m
//  PickerController
//
//  Created by 谭谭伟 on 17/3/2.
//  Copyright © 2017年 谭谭伟. All rights reserved.
//

#import "TWUploadImage.h"

@implementation TWUploadImage

static TWUploadImage *instance = nil;

//单例实现
+ (TWUploadImage *)shareUploadImage {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[TWUploadImage alloc]init];
    });
    return instance;
}

- (void)showAlertControllerInFatherViewController:(UIViewController *)fatherViewController delegate:(id<TW_uploadImageDelegate>)delegate {
    instance.uploadDelegate = delegate;
    _fatherViewController = fatherViewController;
    
    
    //添加照片
    UIImagePickerController *pictureController = [[UIImagePickerController alloc]init];
    pictureController.delegate = self;
    //创建提示框，提示选择相册还是相机
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请选择打开方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    
    //创建相机选项
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //设置点击后进入相册
        pictureController.sourceType = UIImagePickerControllerSourceTypeCamera;
        //设置进入方式
        pictureController.allowsEditing = YES;
        pictureController.modalPresentationStyle = UIModalPresentationFullScreen;
        [_fatherViewController presentViewController:pictureController animated:YES completion:nil];
        
    }];
    
    //创建相册选项
    UIAlertAction *pohotoAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //设置点击后进入相册
        pictureController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        pictureController.allowsEditing = YES;
        //设置进入方式
        pictureController.modalPresentationStyle = UIModalPresentationFullScreen;
        [_fatherViewController presentViewController:pictureController animated:YES completion:nil];
    }];
    
    //取消选项
    UIAlertAction *canelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [_fatherViewController dismissViewControllerAnimated:YES completion:nil];
    }];
    
    //添加行为事件
    [alertController addAction:cameraAction];
    [alertController addAction:pohotoAction];
    [alertController addAction:canelAction];
    
    //弹出提示框
    [_fatherViewController presentViewController:alertController animated:YES completion:nil];

}

//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
//    UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];
//    
//    //上传用户头像
//    if (self.uploadDelegate && [self.uploadDelegate respondsToSelector:@selector(uploadImageDealWith:)]) {
//        [self.uploadDelegate uploadImageDealWith:image];
//    }
//}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo {
    //上传用户头像
    if (self.uploadDelegate && [self.uploadDelegate respondsToSelector:@selector(uploadImageDealWith:)]) {
        [self.uploadDelegate uploadImageDealWith:image];
    }

}
@end
